package com.example.pets_java.service;

import com.example.pets_java.mapper.ArticleMapper;
import com.example.pets_java.mapper.CommentMapper;
import com.example.pets_java.mapper.UserMapper;
import com.example.pets_java.pojo.Article;
import com.example.pets_java.pojo.Comment;
import com.example.pets_java.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    public List<Article> getArticleById(Long id) {
        return articleMapper.getArticleById(id);
    }


    public List<Article> selectArticleList(Article article)
    {
        return articleMapper.selectArticleList(article);
    }

    public void insertArticle(Article article)
    {
        User user = userMapper.findById(Math.toIntExact(article.getAuthId()));

        if(user == null){
            throw new RuntimeException("用户不存在");
        }

        article.setCreateBy(user.getUsername());
        article.setCreateTime(new Date());

        articleMapper.insertArticle(article);
    }

    public void updateArticle(Article article)
    {
        articleMapper.updateArticle(article);
    }

    public void deleteArticleById(Long id)
    {
        articleMapper.deleteArticleById(id);
    }


}
